package ioc

import (
	"fmt"
	"github.com/streadway/amqp"
	"go.uber.org/zap"
	"mts/log_srv/global"
)

// InitRabbitMQ 初始化 RabbitMQ 连接和通道
func InitRabbitMQ() (*amqp.Connection, *amqp.Channel, error) {
	mq := global.ServerConfig.RabbitMQInfo
	conn, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d", mq.User, mq.Pass, mq.Host, mq.Port))
	if err != nil {
		zap.S().Fatalf("RabbitMQ 连接失败: %s", err)
		return nil, nil, err
	}

	ch, err := conn.Channel()
	if err != nil {
		zap.S().Fatalf("Failed to open a channel: %s", err)
		conn.Close()
		return nil, nil, err
	}

	return conn, ch, nil
}
